package com.yida.system.controller;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.yida.common.core.domain.R;
import com.yida.common.core.utils.StringUtils;
import com.yida.common.core.utils.file.FileTypeUtils;
import com.yida.common.core.utils.file.MimeTypeUtils;
import com.yida.common.core.utils.poi.ExcelUtil;
import com.yida.common.core.web.controller.BaseController;
import com.yida.common.core.web.domain.AjaxResult;
import com.yida.common.core.web.page.TableDataInfo;
import com.yida.common.log.annotation.Log;
import com.yida.common.log.enums.BusinessType;
import com.yida.common.security.annotation.RequiresPermissions;
import com.yida.system.api.RemoteFileService;
import com.yida.system.api.domain.SysFile;
import com.yida.system.domain.SysMenu;
import com.yida.system.domain.SysMenuImg;
import com.yida.system.service.ISysMenuImgService;
import com.yida.system.service.ISysMenuService;

/**
 * 菜单图片样式上传页面Controller
 * 
 * @author hmh
 * @date 2023-05-24
 */
@RestController
@RequestMapping("/menuImg")
public class SysMenuImgController extends BaseController {
    @Autowired
    private ISysMenuImgService sysMenuImgService;

    @Autowired
    private RemoteFileService remoteFileService;

    @Autowired
    private ISysMenuService menuService;

    /**
     * 查询菜单图片样式上传页面列表
     */
    @RequiresPermissions("system:img:list")
    @GetMapping("/list")
    public TableDataInfo list(SysMenuImg sysMenuImg) {
        startPage();
        List<SysMenuImg> list = sysMenuImgService.selectSysMenuImgList(sysMenuImg);
        return getDataTable(list);
    }

    /**
     * 导出菜单图片样式上传页面列表
     */
    @RequiresPermissions("system:img:export")
    @Log(title = "菜单图片样式上传页面", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysMenuImg sysMenuImg) {
        List<SysMenuImg> list = sysMenuImgService.selectSysMenuImgList(sysMenuImg);
        ExcelUtil<SysMenuImg> util = new ExcelUtil<SysMenuImg>(SysMenuImg.class);
        util.exportExcel(response, list, "菜单图片样式上传页面数据");
    }

    /**
     * 获取菜单图片样式上传页面详细信息
     */
    @RequiresPermissions("system:img:query")
    @GetMapping(value = "/{imgId}")
    public AjaxResult getInfo(@PathVariable("imgId") Long imgId) {
        return AjaxResult.success(sysMenuImgService.selectSysMenuImgByImgId(imgId));
    }

    /**
     * 新增菜单图片样式上传页面
     */
    @RequiresPermissions("system:img:add")
    @Log(title = "菜单图片样式上传页面", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysMenuImg sysMenuImg) {
        return toAjax(sysMenuImgService.insertSysMenuImg(sysMenuImg));
    }

    /**
     * 修改菜单图片样式上传页面
     */
    @RequiresPermissions("system:img:edit")
    @Log(title = "菜单图片样式上传页面", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysMenuImg sysMenuImg) {
        return toAjax(sysMenuImgService.updateSysMenuImg(sysMenuImg));
    }

    /**
     * 删除菜单图片样式上传页面
     */
    @RequiresPermissions("system:img:remove")
    @Log(title = "菜单图片样式上传页面", businessType = BusinessType.DELETE)
    @DeleteMapping("/{imgIds}")
    public AjaxResult remove(@PathVariable Long[] imgIds) {
        return toAjax(sysMenuImgService.deleteSysMenuImgByImgIds(imgIds));
    }

    /**
     * 菜单图片上传
     */
    @Log(title = "菜单图片样式上传页面", businessType = BusinessType.UPDATE)
    @PostMapping("/imgUrl")
    public AjaxResult imgUrl(@RequestParam("file") MultipartFile file, SysMenuImg sysMenuImg) {
        if (file.isEmpty()) {
            return AjaxResult.error("未获取到上传图片信息！请确认后提交！");
        }
        SysMenu sysMenu = menuService.selectMenuById(sysMenuImg.getMenuId());
        if (StringUtils.isNull(sysMenu)) {
            return AjaxResult.error("未获取到对应菜单数据，请新增后操作！");
        }
        String extension = FileTypeUtils.getExtension(file);
        if (!StringUtils.equalsAnyIgnoreCase(extension, MimeTypeUtils.IMAGE_EXTENSION)) {
            return AjaxResult.error("文件格式不正确，请上传" + Arrays.toString(MimeTypeUtils.IMAGE_EXTENSION) + "格式");
        }
        R<SysFile> fileResult = remoteFileService.upload(file);
        if (StringUtils.isNull(fileResult) || StringUtils.isNull(fileResult.getData())) {
            return AjaxResult.error("文件服务异常，请联系管理员");
        }
        String url = fileResult.getData().getUrl();
        if (sysMenuImgService.updateMenuImg(sysMenuImg.getMenuId(), url)) {
            return AjaxResult.success("图片上传成功！");
        }
        return AjaxResult.error("上传图片失败，请联系管理员");
        // --------------------旧代码保留------------------------------------
        // if (!file.isEmpty()) {
        // SysMenu sysMenu = menuService.selectMenuById(sysMenuImg.getMenuId());
        // if (StringUtils.isNull(sysMenu)) {
        // return AjaxResult.error("未获取到对应菜单数据，请新增后操作！");
        // }
        // String extension = FileTypeUtils.getExtension(file);
        // if (!StringUtils.equalsAnyIgnoreCase(extension, MimeTypeUtils.IMAGE_EXTENSION)) {
        // return AjaxResult.error("文件格式不正确，请上传" + Arrays.toString(MimeTypeUtils.IMAGE_EXTENSION) + "格式");
        // }
        // R<SysFile> fileResult = remoteFileService.upload(file);
        // if (StringUtils.isNull(fileResult) || StringUtils.isNull(fileResult.getData())) {
        // return AjaxResult.error("文件服务异常，请联系管理员");
        // }
        // String url = fileResult.getData().getUrl();
        // if (sysMenuImgService.updateMenuImg(sysMenuImg.getMenuId(), url)) {
        // return AjaxResult.success("图片上传成功！");
        // }
        // }
        // return AjaxResult.error("上传图片异常，请联系管理员");
    }

    @RequiresPermissions("system:img:edit")
    @Log(title = "菜单图片样式上传页面", businessType = BusinessType.UPDATE)
    @PostMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody SysMenuImg sysMenuImg) {
        return toAjax(sysMenuImgService.changeSysMenuImgStatus(sysMenuImg));
    }

    /**
     * 获取菜单图片样式上传页面详细信息
     */
    @RequiresPermissions("system:img:query")
    @GetMapping(value = "/menu/{menuId}")
    public AjaxResult getMenuImgInfo(@PathVariable("menuId") Long menuId) {
        return AjaxResult.success(sysMenuImgService.selectSysMenuImgByMenuId(menuId));
    }
}
